package com.shuw.com.beingfortextproject.base;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import com.shuw.com.beingfortextproject.utils.AppManager;


/**
 * 作者  Administrator
 * 时间  2016/11/4 15:32
 * 文件  Petrochinashx
 * 描述  Activity基类
 */

public class BaseActivity extends AppCompatActivity {

	private ProgressDialog dialog;
	protected Context mContext;
	private InputMethodManager inputMethodManager;

	@Override
	protected void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext=this;
		inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		AppManager.getAppManager().addActivity(this);
	}

	protected void hideSoftKeyboard() {
		if (getWindow().getAttributes().softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) {
			if (getCurrentFocus() != null)
				inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
		}
	}

	public void showLoading(String msg) {
		if (dialog == null)
			dialog = new ProgressDialog(this);
		dialog.setMessage(msg);
		dialog.show();
	}

	public void hideLoading() {
		if (dialog != null && dialog.isShowing()) {
			dialog.dismiss();
			dialog = null;
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		AppManager.getAppManager().removeActivity(this);
//		RefWatcher refWatcher = BaseApplication.getRefWatcher(this);
//		refWatcher.watch(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId()==android.R.id.home){
			finish();
		}
		return true;
	}

	protected void showToast(String msg){
		Toast.makeText(BaseActivity.this,msg, Toast.LENGTH_SHORT).show();
	}

	protected void setToolbar(Toolbar toolbar){
		setSupportActionBar(toolbar);
		getSupportActionBar().setDisplayShowTitleEnabled(false);  // 需要隐藏Toolbar本身的title
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
	}

}
